<?php
declare(strict_types=1);

namespace App\Logic\User\Service\User;

use App\Constant\CacheKey;
use App\Exceptions\ScoreException;
use App\Library\RedisClient;
use App\Library\SnowFlakeId;
use App\Logic\User\Repository\User\UserRepository;
use App\Logic\User\Repository\User\UserScoreHistoryRepository;
use App\Logic\User\Service\BaseService;
use App\Logic\User\Service\UserServiceInterface;
use Closure;
use Illuminate\Support\Facades\Cache;
use function Symfony\Component\Translation\t;

class UserScoreHistoryService extends BaseService implements UserServiceInterface
{
    public static function searchWhere(array $requestParams): Closure
    {
        return function ($query) use ($requestParams) {
            extract($requestParams);
            $query->where("user_uid", "=", $requestParams["user_uid"]);
        };
    }

    public function serviceSelect(array $requestParams): array
    {
        $requestParams["user_uid"] = $this->getUserUid();
        return (new UserScoreHistoryRepository())->repositorySelect(
            self::searchWhere($requestParams),
            (int)($requestParams["size"] ?? 20),
            ["title", "created_at as in_time", "score", "type"]
        );
    }

    public function serviceAdd(array $requestParams): array
    {
        return [];
    }

    /**
     * 用户积分变动
     * @param string $userUid 用户uid
     * @param float $score 变动积分记录
     * @param string $title 积分名称
     * @param int $type 积分变动类型1增加2减少
     * @return bool
     * @throws ScoreException
     * @author 兔兔答题
     * @link https://www.tutudati.com
     * @date 2024/3/23 19:20
     */
    public static function serviceUserScoreUpdate(float $score, string $title, string $userUid = "", int $type = 1): bool
    {
        if (empty($userUid)) {
            $userUid = (new self())->getUserUid();
        }
        $userScoreCache = Cache::get(CacheKey::$scoreKey . $userUid);
        if (empty($userScoreCache)) {
            $userScore = (new UserRepository())->repositoryFind(function ($query) use ($userUid) {
                $query->where("uid", "=", $userUid);
            }, ["score"]);
            if (!empty($userScore)) {
                Cache::put(CacheKey::$scoreKey . $userUid, $userScore["score"]);
            }
            $userScoreCache = 0;
        }
        if ($type === 2 && $userScoreCache < $score) {
            throw new ScoreException("积分不足");
        }
        $result = (new UserScoreHistoryRepository())->repositoryCreate([
            "uid" => SnowFlakeId::getId(),
            "user_uid" => $userUid,
            "score" => $score,
            "title" => $title,
            "type" => $type,
        ]);
        if ($result && $type === 1) {// 增加积分
            return Cache::put(CacheKey::$scoreKey . $userUid, $userScoreCache + $score);
        }
        if ($result && $type === 2) {// 减少积分
            return Cache::put(CacheKey::$scoreKey . $userUid, $userScoreCache - $score);
        }
        return false;
    }

    public function serviceCreate(array $requestParams): array
    {
        return [];
    }

    public function serviceUpdate(array $requestParams): array
    {
        return [];
    }

    public function serviceDelete(array $requestParams): int
    {
        return 0;
    }

    public function serviceFind(array $requestParams): array
    {
        return [];
    }
}
